// pages/goEvaluateDetail/goEvaluateDetail.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    waitEvaluate: [],
    show: false,
    evaluateNum: 0,
    evaluateValue: "",
    score: 5,
    id: "",
    myOrderId: "",
    index: 0,
    myOrderIndex: 0,
    waitUpdataList: [],
    nullShow: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var list = JSON.parse(options.List)
    if (list.length != 0) {
      this.setData({
        waitEvaluate: list
      })
    } else {
      this.setData({
        nullShow: true
      })
    }

  },
  goToEvaluate(e) {
    console.log(e)
    this.setData({
      show: true,
      id: e.currentTarget.dataset.id,
      myOrderId: e.currentTarget.dataset.myorderid,
      index: e.currentTarget.dataset.index,
      myOrderIndex: e.currentTarget.dataset.myorderindex,
    });
    app.getOne("orderList", this.data.myOrderId)
      .then(res => {
        console.log('getone', res.data.myOrder)
        this.data.waitUpdataList = res.data.myOrder
      })
  },
  getEvaluateDetail(e) {
    if (e.detail.cursor > 500) {
      return
    }
    this.setData({
      evaluateNum: e.detail.cursor,
      evaluateValue: e.detail.value
    })
  },
  onChange(e) {
    this.setData({
      score: e.detail
    })
  },
  enterEvaluate() {
    let name = ''
    let image = ''
    let that = this
    try {
      let value = wx.getStorageSync('userInfo')
      name = value.name
      image = value.image
    } catch (e) {}
    if (this.data.evaluateNum == 0) {
      wx.showToast({
        title: '请输入评价内容',
        icon: 'none'
      })
    } else {
      wx.cloud.callFunction({
          // 云函数名称
          name: 'addEvaluate',
          // 传给云函数的参数
          data: {
            evaluate: this.data.evaluateValue,
            score: this.data.score,
            id: this.data.id,
            image: image,
            name: name,
          },
        })
        .then(res => {
          that.delWaitEvaluate(that.data.myOrderId, that.data.myOrderIndex)
        })
        .catch(console.error)
    }
  },
  delWaitEvaluate(myOrderId, myOrderIndex) {
    let that = this
    let list = this.data.waitEvaluate
    let waitList = this.data.waitUpdataList
    for (let i = 0; i < that.data.waitUpdataList.length; i++) {
      if (that.data.waitUpdataList[i].myOrderIndex == myOrderIndex) {
        that.data.waitUpdataList[i].evaluate = "已评价"
      }
    }

    wx.cloud.database().collection('orderList').doc(myOrderId).update({
      data: {
        myOrder: waitList
      },
      success: function (res) {
        list.splice(that.data.index, 1)
        if (list.length != 0) {
          that.setData({
            waitEvaluate: list,
            show: false,
            evaluateValue: "",
            evaluateNum: 0
          })
        } else {
          that.setData({
            waitEvaluate: list,
            show: false,
            evaluateValue: "",
            evaluateNum: 0,
            nullShow: true
          })
        }

        wx.showToast({
          title: '评价成功',
          icon: 'none'
        })
      }
    })
  },
  goToProductDetail(e) {
    wx.navigateTo({
      url: '../productDetail/productDetail?id=' + e.currentTarget.dataset.id,
    })
  },
  onClose() {
    this.setData({
      show: false,
      evaluateValue: "",
      evaluateNum: 0
    });
  }
})